package com.atguigu.service.impl;

import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.SentinelWebInterceptor;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.feign.ProductFeignClient;
import com.atguigu.order.bean.Order;
import com.atguigu.product.bean.Product;
import com.atguigu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @SentinelResource(value = "getOrderById",blockHandler = "creatOrderFallback")
    @Override
    public Order getOrderById(Long userId, Long productId){

        Product product = getProductFormRemote(productId);
        Order order = new Order();
        order.setId(userId);
        order.setProductId(productId);
        order.setProductName("华为手机");
        order.setCount(1);
        order.setMoney(product.getPrice().multiply(new BigDecimal(1)));
        order.setStatus(1);
        order.setCreateTime("2020-03-01 12:00:00");
        // 远程查询商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }

    // 兜底回调
    public Order creatOrderFallback(Long userId, Long productId, BlockException e){

        Order order = new Order();
        order.setCount(0);
        order.setCreateTime(String.valueOf(new Date().getTime()));
        order.setStatus(1);
        order.setMoney(new BigDecimal(0));
        order.setProductId(productId);
        order.setId(userId);
        order.setProductName("异常手机");
        return order;

    }


    // 1. 获取商品服务所在的所有IP和端口号
    private Product getProductFormRemote(Long ProductId){
        // 使用feign 远程调用
        Product product = productFeignClient.getProductById(ProductId);



        // (1) 通过loadBalancer负载均衡器，获取所有服务实例
        ServiceInstance choose = loadBalancerClient.choose("product");
        // (2) 通过discoveryClient，获取所有服务实例
        // List<ServiceInstance> instances = discoveryClient.getInstances("product");
        // ServiceInstance instance = instances.get(0);

        // (1) String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + ProductId;
        // (2) String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + ProductId;

        // (3) 通过注解到restTemplate实现远程调用
        String url = "http://product/product/" + ProductId; // 第一个product会被动态替换成ip
        log.info("远程请求：{}",url); // http://localhost:9000/product/3
        // 2. 发送请求
        //Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
